package com.starlink.databaseservice.controller;

import com.starlink.databaseservice.mapper.PostMapper;
import com.starlink.databaseservice.model.Post;
import com.starlink.databaseservice.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/")
@Controller
public class IndexController {
    @Autowired
    private PostService postService;

    private PostMapper postMapper = PostMapper.INSTANCE;

    @GetMapping
    public ModelAndView index() {
        /*if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 15;
        }
        Random random = new Random();
        Page<Post> pageInfo = postService.getList(page, size);

        BaseResult<PostDto> postDtoBaseResult = BaseResult.pageResult(pageInfo.getContent().stream().map(postMapper::postToPostDto).toList(),
                pageInfo.getTotalElements());*/
        Post post = postService.getPostRandom();
        ModelAndView mav = new ModelAndView("index/index");
        mav.addObject("post", postMapper.postToPostDto(post));
        return mav;
    }
}
